package com.zhao.reggie3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhao.reggie3.common.ProductException;
import com.zhao.reggie3.damain.Category;
import com.zhao.reggie3.damain.Dish;
import com.zhao.reggie3.damain.Setmeal;
import com.zhao.reggie3.dao.CategoryDao;
import com.zhao.reggie3.service.CategoryService;
import com.zhao.reggie3.service.DishService;
import com.zhao.reggie3.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: zhao
 * @time: 2022/8/17 14:59
 */
@Service
@Slf4j
public class CategoryServiceimpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /*
     * @description: 根据id删除,删除之前需要进行判断
     * @params:
     * @return:
     */
    @Override
    public void remove(Long id) {
        //查询当前分类是否关联了菜品，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Dish> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Dish::getCategoryId,id);

        int count = dishService.count(lqw1);

        if (count>0){
            throw new ProductException("当前分类下关联了菜品，不能删除");
        }
        //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Setmeal::getCategoryId,id);

        int count1 = setmealService.count(lqw2);

        if (count1>0){
            throw new ProductException("当前分类下关联了套餐，不能删除");
        }

        super.removeById(id);

    }
}